package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private TagMapper tagMapper;

    public PageInfo queryForList(QueryObject qo){
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderby());
        List<StrategyComment> list = commentMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(StrategyComment comment,String[] tags) {
        if (comment.getId() != null) {
            commentMapper.updateByPrimaryKey(comment);
        }else {
            comment.setCreateTime(new Date());
            commentMapper.insert(comment);
            for (String t : tags) {
                Tag tag = new Tag();
                tag.setName(t);
                tagMapper.insert(tag);
                commentMapper.insertRelation(comment.getId(),tag.getId());
            }
        }
    }

    public List<Tag> queryForTagList(Long strategyId) {
        return tagMapper.selectForTagList(strategyId);
    }
}
